﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class HangMucService : IService<HangMuc>
    {
        private readonly IEntityRepository<HangMuc> _hangMucRepository;
        public HangMucService(IEntityRepository<HangMuc> hangMucRepository)
        {
            _hangMucRepository = hangMucRepository;
        }

        public HangMuc Create(HangMuc t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _hangMucRepository.InsertOnCommit(t);
                _hangMucRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public HangMuc Update(HangMuc t)
        {
            HangMuc item;
            try
            {
                item = _hangMucRepository.Get(t.Id);
                item.TenCongTrinh = t.TenCongTrinh;
                item.HangSanXuat_Id = t.HangSanXuat_Id;
                item.BacChiuLua_Id = t.BacChiuLua_Id;
                item.DienTichM2 = t.DienTichM2;
                item.SoTang = t.SoTang;
                item.DienTichM3 = t.DienTichM3;
                item.SoPhong = t.SoPhong;
                item.TenChatChay = t.TenChatChay;
                item.DienTichChatChayM2 = t.DienTichChatChayM2;
                //item.Created = DateTime.Now;
                //item.CreatedBy_Id = t.CreatedBy_Id;
                _hangMucRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public HangMuc Detail(Guid id)
        {
            var item = _hangMucRepository.Get(id);
            return item;
        }

        public List<HangMuc> ToList()
        {
            var item = _hangMucRepository.GetAll().ToList();
            return item;
        }

        public void Delete(HangMuc t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _hangMucRepository.DeleteOnCommit(t);
                _hangMucRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
